home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Compression / Opener / Source / utils / arc / tmclock.c < prev   
C/C++ Source or Header  |  1991-10-02  |  2KB  |  101 lines

  1. /*
  2.  * Stolen from Jef Poskanzer's tws time library, which was stolen from
  3.  * Marshall Rose's MH Message Handling system...
  4.  *
  5.  * tmclock() will convert time from a tm struct back to a clock value.
  6.  * tmjuliandate() converts a tm struct to its julian day number.
  7.  * tmsubdayclock() takes hours, minutes, and seconds from a tm struct
  8.  * and returns the number of seconds since midnight of that day. (?)
  9.  *  -- Howard Chu, August 1 1988      hyc@umix.cc.umich.edu, umix!hyc
  10.  */
  11.  
  12. /* $Header: tmclock.c,v 1.2 88/08/01 14:34:38 hyc Exp $ */
  13.  
  14. /* Julian day number of the Unix* clock's origin, 01 Jan 1970. */
  15. #define JD1970 2440587L
  16. #define    CENTURY    19
  17. #if    BSD
  18. #include <sys/time.h>
  19. int    daylight;
  20. long    timezone;
  21. #else
  22. #include <time.h>
  23. #endif
  24.  
  25. long
  26. tmjuliandate( tm )
  27. struct tm *tm;
  28.     {
  29.     register int mday, mon, year;
  30.     register long a, b;
  31.     double jd;
  32.  
  33.     if ( (mday = tm -> tm_mday) < 1 || mday > 31 ||
  34.         (mon = tm -> tm_mon + 1) < 1 || mon > 12 ||
  35.         (year = tm -> tm_year) < 1 || year > 10000 )
  36.     return ( -1L );
  37.     if ( year < 100 )
  38.     year += CENTURY * 100;
  39.  
  40.     if ( mon == 1 || mon == 2 )
  41.     {
  42.     --year;
  43.     mon += 12;
  44.     }
  45.     if ( year < 1583 )
  46.     return ( -1L );
  47.     a = year / 100;
  48.     b = 2 - a + a / 4;
  49.     b += (long) ( (double) year * 365.25 );
  50.     b += (long) ( 30.6001 * ( (double) mon + 1.0 ) );
  51.     jd = mday + b + 1720994.5;
  52.     return ( (long) jd );
  53.     }
  54.  
  55.  
  56. long
  57. tmsubdayclock( tm )
  58. struct tm *tm;
  59.     {
  60.     register int sec, min, hour;
  61.     register long result;
  62. #if    BSD
  63.     {
  64.        struct timeval tp;
  65.        struct timezone tzp;
  66.  
  67.        gettimeofday(&tp, &tzp);
  68.        daylight=tzp.tz_dsttime;
  69.        timezone=tzp.tz_minuteswest*(-60);
  70.     }
  71. #endif
  72.     if ( (sec = tm -> tm_sec) < 0 || sec > 59 ||
  73.         (min = tm -> tm_min) < 0 || min > 59 ||
  74.         (hour = tm -> tm_hour) < 0 || hour > 23 )
  75.     return ( -1L );
  76.  
  77.     result = ( hour * 60 + min ) * 60 + sec;
  78.     result -= timezone;
  79.     if ( daylight )
  80.     result -= 60 * 60;
  81.  
  82.     return ( result );
  83.     }
  84.  
  85.  
  86. long
  87. tmclock( tm )
  88. struct tm *tm;
  89.     {
  90.     register long jd, sdc, result;
  91.  
  92.     if ( ( jd = tmjuliandate( tm ) ) == -1L )
  93.     return ( -1L );
  94.     if ( ( sdc = tmsubdayclock( tm ) ) == -1L )
  95.     return ( -1L );
  96.  
  97.     result = ( jd - JD1970 ) * 24 * 60 * 60 + sdc;
  98.  
  99.     return ( result );
  100.     }
  101.